package leetcode.秋招.奇安信;

import org.junit.Test;

import java.util.*;


public class Solution1 {

    @Test
    public void test() {
        String[] permutation = Permutation(new char[]{'0', '1', '2', '3', 'A'}, 3);
    }
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param seed char字符型一维数组 字符种子
     * @param size int整型 验证码长度
     * @return string字符串一维数组
     */
    private List<String> res;
    private Set<String> set;
    char[] seed;
    public String[] Permutation (char[] seed, int size) {
        // write code here
        this.res = new ArrayList<>();
        this.seed = seed;
        set = new HashSet<>();
        dfs(0, size, new StringBuilder());
        String[] ans = new String[res.size()];
        res.sort(String :: compareTo);
        for (int i = 0; i < res.size(); i++) {
            ans[i] = res.get(i);
        }
        return ans;
    }

    private void dfs(int index, int size, StringBuilder builder) {
        if (index == size) {
            if (set.add(builder.toString()))
                res.add(builder.toString());
            return;
        }
        for (int i = 0; i < seed.length; i++) {
            if (!builder.toString().contains(seed[i] + "")) {
                StringBuilder sub = new StringBuilder(builder);
                sub.append(seed[i]);
                dfs(index + 1, size, sub);
            }
        }
    }
}
